<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="padding-top:10px;padding-left:15px;">
      
      <el-row>
        <!--表格内容栏-->
        <el-table
          :data="pageResult"
          style="width: 100%"
          :stripe="stripe"
          :max-height="countHeight(maxHeight)"
          :size="size"
           align="left"
          :header-cell-style="{ 'color': '#FFF','background-color': '#2F4554'}">
          
          <el-table-column
            label="结算优先级类型"
            width="800"
            align="center"
            prop="promotionType"
            :formatter="typeFormat"
            >
          </el-table-column>
          
          <el-table-column
            label="显示顺序"
            width="800"
            align="center"
            prop="sort">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="handleUpdateSort(0,scope.row,scope.$index)" :disabled="(pageResult && pageResult.length > 0 && pageResult.length ==1) || scope.$index == 0">上移</el-button>
              <el-button size="mini" type="primary" @click="handleUpdateSort(1,scope.row,scope.$index)" :disabled="(pageResult && pageResult.length > 0 && pageResult.length ==1) || (scope.$index == pageResult.length -1)">下移</el-button>
            </template>
          </el-table-column>
          <el-table-column>
          </el-table-column>
        </el-table>
      </el-row>
      <!--分页栏-->
      <div class="toolbar" style="padding:10px;">
        <el-pagination
          layout="total, prev, pager, next, jumper,sizes"
          @current-change="refreshPageRequest"
          :current-page="pageRequest.pageNum"
          :page-size="pageRequest.pageSize"
          :total="totalSize"
          style="float:right;"
          :page-sizes="[5,10, 20, 30, 40]"
          @size-change="handleSizeChange"
        ></el-pagination>
    </div>



    </div>
  </div>
</template>

<script>
  import PopupTreeInput from "@/components/PopupTreeInput";
  import KtButton from "@/views/Core/KtButton";
  import ElRow from "element-ui/packages/row/src/row";
  export default {
    components: {
      ElRow,
      PopupTreeInput,
      KtButton,
    },
    data() {
      return {
        size: "small",
        labelPosition: 'left',
        stripe:true,
        maxHeight: 900,
        filters: {
          name:"",//广告名称
          type:"",//素材类型
          startDate:"",//有效期开始时间
          endDate:"",//有效期结束时间
          status:"0",

        },
        columns: [],
        filterColumns: [],
        pageRequest: { pageNum: 1, pageSize: 10 },
        pageResult: [],
        totalSize:0,
        options: [],
        activeName:"0",//tab默认选中生效中 0
        adId:this.$route.params.id,//广告codeid
        startDateDisabled:{},
        endDateDisabled:{},
      };
    },
    methods: {
      changeStartDate:function () {
        var this_= this;
        this_.endDateDisabled = {
          disabledDate: (time)=> {
            if (this_.filters.startDate){
              return time.getTime() < new Date(this_.filters.startDate).getTime();
            }
          },
        };
      },
      changeendDate:function () {
        var this_= this;
        this_.startDateDisabled = {
          disabledDate: (time)=> {
            if (this_.filters.endDate){
              return time.getTime() > new Date(this_.filters.endDate).getTime();
            }
          },
        };
      },
      // 获取分页数据
      findPage: function(filters) {
        var this_ = this;
        if (filters == undefined || filters == null) {
          filters = {};
        }
        if (this_.adId){
          filters.adCodeId = this_.adId;
        }
        filters.start = this.pageRequest.pageNum;
        filters.limit = this.pageRequest.pageSize;
        this.utils.request.queryActivityPriorityPage(filters, function(res) {
          if (res && res.code == '0000' && res.data.rows) {
            this_.pageResult = res.data.rows;
            this_.totalSize = Number(res.data.total);
          }else {
            this_.pageResult = [];
            this_.totalSize = 0;
          }
        });
      },

      // 类型格式化
      typeFormat: function(row, column, cellValue, index) {
        if (cellValue == 1) {
          return "单一促销";
        }else if(cellValue == 2){
          return "满量促销";
        }else if(cellValue == 3){
          return "满额促销";
        }else if(cellValue == 4){
          return "秒杀";
        }else if(cellValue == 5){
          return "赠送类促销";
        }else if(cellValue == 6){
          return "满量组合促销促销";
        }else if(cellValue == 7){
          return "单品X件优惠";
        }else if(cellValue == 8){
          return "换购";
        }else if(cellValue == 9){
          return "预售";
        }else if(cellValue == 10){
          return "拼团";
        }else if(cellValue == 11){
          return "优惠券";
        }else if(cellValue == 12){
          return "红包";
        }else if(cellValue == 13){
          return "抽奖";
        }
        
      },
      reset: function() {
        this.$refs["filters"].resetFields();
        this.findPage(this.filters);
      },
      //切换状态tab 0：生效中 1：待生效 2：已过期
      handleClick(tab) {
        this.filters.status = tab.name;
        this.findPage(this.filters)
      },
      countHeight(height) {
        var winHeight =
          document.body.clientHeight || document.documentElement.clientHeight;
        if (height > winHeight * 0.7) {
          height = winHeight * 0.7;
        }
        return height;
      },
      // 换页刷新
      refreshPageRequest: function(pageNum) {
        this.pageRequest.pageNum = pageNum;
        this.findPage(this.filters);
      },
      // 换页刷新
      handleSizeChange: function(pageSize) {
        this.pageRequest.pageSize = pageSize;
        this.pageRequest.pageNum = 1;
        this.findPage(this.filters);
      },
      
      //排序,type：0 上移 1 下移
      handleUpdateSort:function (type,row,index) {
        var oldSort = row.sort,oldId = row.id;
        var newSort = '',newId = '';
        if (type == 0){
          newId =  this.pageResult[index-1].id;
          newSort = this.pageResult[index-1].sort;
        }else {
          newId =  this.pageResult[index+1].id;
          newSort = this.pageResult[index+1].sort;
        }
        var this_ = this,
          params = {
            oldSort:oldSort,
            oldId:oldId,
            newSort:newSort,
            newId:newId
          };
        console.log(params);
        this.utils.request.updateActivityPrioritySort(params,function (res) {
          if (res && res.code == '0000'){
            this_.findPage()
          }else {
            this_.$message.error(res.msg || '删除失败!');
          }
        });
      },
    },

    mounted() {
      this.findPage(this.filters);
    }
  };
</script>

<style scoped>
</style>
